package com.minshenglife.zanzan.web.weixin.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.minshenglife.commons.security.utils.AccessTokenUtils;
import com.minshenglife.commons.security.utils.pp.UserIdentify;
import com.minshenglife.commons.security.utils.pp.UserIdentify.UserType;
import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.pub.message.resp.NewsMessage;
import com.minshenglife.zanzan.pub.message.resp.model.Article;
import com.minshenglife.zanzan.pub.utils.WXKeys;
import com.minshenglife.zanzan.service.AccessTokenService;
import com.minshenglife.zanzan.service.RedFlowerService;
import com.minshenglife.zanzan.service.ReviewService;
import com.minshenglife.zanzan.service.UserService;

/**
 * chuli菜单事件
 * @author huodd
 *
 */
@Component
public class WXMenuEventService {
	 Logger log = LoggerFactory.getLogger(WXMenuEventService.class);
	 
	
	@Resource
	ReviewService reviewService;
	@Resource
	UserService userService;
	@Resource
	RedFlowerService redFlowerService;
	@Resource
	AccessTokenService accessTokenService;
	
	/**
	 * 处理菜单事件
	 * @param key 事件key值
	 * @param access_token 事件请求者的微信号access_token
	 * @param baseUrl 
	 * @return
	 */
	public NewsMessage getEventResult(String key,String userOpenId,NewsMessage message) {
		//校验请求者是否已经绑定员工号 
		User user=userService.getUserByWXCode(userOpenId);
		String access_token=null;
		if(user!=null){
		 access_token=AccessTokenUtils.newToken(new UserIdentify(UserType.weixin,userOpenId), user.getIdNo());
		 accessTokenService.insertAccessToken(user,access_token);//插入页面时效
		}
		List<Article> list=new ArrayList<Article>();
		if(user==null){
			//用时间串生成access_token
			 access_token=AccessTokenUtils.newToken(new UserIdentify(UserType.weixin,userOpenId), String.valueOf(new Date().getTime()));
			 accessTokenService.insertAccessTokenOfNotUser(userOpenId,access_token);//插入页面时效
			list.add(new Article("请绑定员工号",null,null,WXKeys.WX_BASEURL+"/jsp/weixin/register.jsp?access_token="+access_token));//"/user/gotoRegister.do?access_token="+access_token
		}
		
		else if(WXKeys.WX_ZZMSR_BTN_MENU_REDRANKING.equals(key)){
			list.add(new Article("红花榜",null,WXKeys.WX_BASEURL+"/images/weixin/secondMenuPic/redFlowerRank.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/redFlowerRank.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_SENDRANKING.equals(key)){
			list.add(new Article("送花榜",null,WXKeys.WX_BASEURL+"/images/weixin/secondMenuPic/sendFlowerRank.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/sendFlowerRank.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_PROGRESSRANKING.equals(key)){
			list.add(new Article("进步榜",null,WXKeys.WX_BASEURL+"/images/weixin/secondMenuPic/progressRank.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/progressRank.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_INTEGRALRANKING.equals(key)){
			list.add(new Article("积分榜",null,WXKeys.WX_BASEURL+"/images/weixin/secondMenuPic/integralRank.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/integralRank.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_MYREDFLOWER.equals(key)){
			list.add(new Article("我的红花",null,WXKeys.WX_BASEURL+"/images/weixin/secondMenuPic/myFlower.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/myFlower.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_MYTEGRAL.equals(key)){
			list.add(new Article("我的积分",null,WXKeys.WX_BASEURL+"/images/weixin/secondMenuPic/myIntegral.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/myIntegral.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_TOSENDFLOWER.equals(key)){
			list.add(new Article("送红花",null,null,WXKeys.WX_BASEURL+"/jsp/weixin/searchEmp.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_MYTOCAO.equals(key)){
			list.add(new Article("我的吐槽",null,WXKeys.WX_BASEURL+"/images/weixin/secondMenuPic/myTucao.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/myEndPlate.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_MYREVIEW.equals(key)){
			list.add(new Article("我的评论",null,WXKeys.WX_BASEURL+"/images/weixin/secondMenuPic/myReview.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/myReview.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_MYINFO.equals(key)){
			list.add(new Article("个人信息",null,WXKeys.WX_BASEURL+"/images/weixin/secondMenuPic/personInfo.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/myInfo.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_WORKCON.equals(key)){
			list.add(new Article("工作日志",null,null,WXKeys.WX_BASEURL+"/jsp/weixin/workConMainPage.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_GAME.equals(key)){
			list.add(new Article("游戏大厅",null,WXKeys.WX_BASEURL+"/images/weixin/secondMenuPic/gameHall.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/gameHall.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_TUCAO.equals(key)){
			list.add(new Article("吐槽版",null,WXKeys.WX_BASEURL+"/images/weixin/secondMenuPic/tucaoBlank.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/endPlateList.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_NEWTHING.equals(key)){
			list.add(new Article("新鲜事",null,WXKeys.WX_BASEURL+"/images/weixin/secondMenuPic/newFresh.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/freshNews.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_HELPREPLAY.equals(key)){
			list.add(new Article("帮助",null,WXKeys.WX_BASEURL+"/images/weixin/secondMenuPic/help.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/helpReplay.jsp?access_token="+access_token));
		}
		
		else if(WXKeys.WX_ZZMSR_BTN_MENU_RANKING.equals(key)){
			list.add(new Article("排行榜",null,WXKeys.WX_BASEURL+"/images/weixin/header.jpg",null));
			list.add(new Article("红花榜",null,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/redFlowerRank.jsp?access_token="+access_token));
			list.add(new Article("送花榜",null,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/sendFlowerRank.jsp?access_token="+access_token));
			list.add(new Article("进步榜",null,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/progressFlowerRank.jsp?access_token="+access_token));
			list.add(new Article("积分榜",null,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/integralRank.jsp?access_token="+access_token));
			list.add(new Article("绑定员工号",null,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/register.jsp?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_ME.equals(key)){
			list.add(new Article("我",null,WXKeys.WX_BASEURL+"/images/weixin/header.jpg",null));
			list.add(new Article("我的红花",null
					,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg"
					,WXKeys.WX_BASEURL+"/jsp/weixin/sendFlowerRank.jsp?access_token="+access_token));
			list.add(new Article("我的积分",null,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/sendFlowerRank.jsp?access_token="+access_token));
			list.add(new Article("我的吐槽",null,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/myEndPlate.jsp?access_token="+access_token));
			list.add(new Article("我的评论",null,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/myReviews.jsp?access_token="+access_token));
			list.add(new Article("个人信息",null,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/otherPersonInfo.jsp?access_token="+access_token));
			list.add(new Article("页面模型",null,WXKeys.WX_BASEURL+"/demo/img/phone.jpg",WXKeys.WX_BASEURL+"/demo/index.html?access_token="+access_token));
		}else if(WXKeys.WX_ZZMSR_BTN_MENU_MORE.equals(key)){
			list.add(new Article("更多",null,WXKeys.WX_BASEURL+"/images/weixin/header.jpg",null));
			list.add(new Article("游戏大厅",null,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/gameHall.jsp?access_token="+access_token));
			list.add(new Article("吐槽版",null,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/endPlateList.jsp?access_token="+access_token));
			list.add(new Article("新鲜事",null,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/freshNews.jsp?access_token="+access_token));
			list.add(new Article("帮助",null,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/helpReplay.jsp?access_token="+access_token));
			list.add(new Article("demo抽奖",null,WXKeys.WX_BASEURL+"/images/weixin/defualt.jpg",WXKeys.WX_BASEURL+"/jsp/weixin/luckDraw.jsp?access_token="+1));
			
		}
		message.setArticles(list);
		message.setArticleCount(list.size());
		return message;
	}
	
	
	
	public static void main(String[] sdf){
		String content="@lisi|adfsadfdd";
		String str=content.substring(1);
		System.out.println(str);
		System.out.println(str.indexOf("|"));
		System.out.println(str.substring(0,str.indexOf("|")));
		String[] arr=str.split("\\|");
		System.out.println(arr.length);
		
		System.out.println((content.substring(1, content.length())).split("\\|").length);
		
		System.out.println(Float.MAX_VALUE);
		System.out.println(Double.MAX_VALUE);
		int a=(int)(1.2*10-1.2*10%10)/10;
		double c=1.2;
		int b=(int)c/1;
		System.out.println((int)c);
		System.out.println(UserType.weixin);
		System.out.println("#\u81EA\u6D4B");
	}


	
}
